package com.ruoyi.common.serializer;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ruoyi.common.annotation.OssUrl;
import com.ruoyi.common.core.domain.vo.SysOssVo;
import com.ruoyi.common.core.service.OssService;
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2021/5/7 17:43
 */
@NoArgsConstructor
public class OssUrlSerializer extends JsonSerializer<String> implements ContextualSerializer {

    private boolean fullInfo;

    public OssUrlSerializer(boolean fullInfo) {
        this.fullInfo = fullInfo;
    }

    @Override
    public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (StrUtil.isNotBlank(value)) {
            List<Long> ossIds = Arrays.stream(value.split(",")).map(Long::parseLong).collect(Collectors.toList());
            if (CollectionUtil.isEmpty(ossIds)) {
                jsonGenerator.writeString(value);
            } else {
                OssService ossService = SpringUtils.getBean(OssService.class);
                List<SysOssVo> list = ossService.listByIds(ossIds);
                if (fullInfo) {
                    jsonGenerator.writeObject(list);
                } else {
                    List<String> urls = list.stream().map(SysOssVo::getUrl).collect(Collectors.toList());
                    jsonGenerator.writeObject(urls);
                }
            }
        } else {
            jsonGenerator.writeString(value);
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty != null) { // 为空直接跳过
            OssUrl ossUrl = beanProperty.getAnnotation(OssUrl.class);
            if (ossUrl == null) {
                ossUrl = beanProperty.getContextAnnotation(OssUrl.class);
            }
            if (ossUrl != null) {
                return new OssUrlSerializer(ossUrl.fullInfo());
            }
        }
        return serializerProvider.findNullValueSerializer(null);
    }

    @Override
    public void serializeWithType(String value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException {
        WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, typeSer.typeId(value, JsonToken.VALUE_STRING));
        serialize(value, gen, serializers);
        typeSer.writeTypeSuffix(gen, typeIdDef);
    }
}
